package com.csu.letaobackend.serviceM.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.csu.letaobackend.bean.*;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.persistence.ItemDao;
import com.csu.letaobackend.persistence.PictureDao;
import com.csu.letaobackend.persistence.ShopDao;
import com.csu.letaobackend.persistence.SwiperitemDao;
import com.csu.letaobackend.serviceM.SwiperitemMService;
import com.csu.letaobackend.vo.SwiperitemVO;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@MapperScan("com.csu.letaobackend.persistence")
public class SwiperitemMServiceImpl implements SwiperitemMService {

      @Autowired
      SwiperitemDao swiperitemDao;

      @Autowired
      PictureDao pictureDao;

      @Autowired
      ItemDao itemDao;

      @Autowired
      ShopDao shopDao;


      @Override
      public CommonResponse<IPage<SwiperitemVO>> getAllSwiperitems(int pageid, int pagesize) {

            IPage<Swiperitem> swiperitemIPage = new Page<>(pageid, pagesize);
            swiperitemDao.selectPage(swiperitemIPage, null);

            List<SwiperitemVO> swiperitemVOList = new ArrayList<>();
            for (Swiperitem swiperitem : swiperitemIPage.getRecords()) {
                  swiperitemVOList.add(entityToVO(swiperitem));
            }
            IPage<SwiperitemVO> swiperitemVOIPage = new Page<>(pageid, pagesize);
            swiperitemVOIPage.setTotal(swiperitemIPage.getTotal());
            swiperitemVOIPage.setRecords(swiperitemVOList);
            return CommonResponse.createForSuccess(swiperitemVOIPage);
      }

      @Override
      public CommonResponse<IPage<SwiperitemVO>> searchSwiperitem(String slogan, String shopname, int pageid, int pagesize) {

            IPage<Swiperitem> swiperitemIPage = new Page<>(pageid, pagesize);

            QueryWrapper<Shop> queryWrapper1 = new QueryWrapper();
            queryWrapper1.like("name", shopname);
            List<Integer> list = new ArrayList<>();
            for (Shop shop : shopDao.selectList(queryWrapper1)) {
                  list.add(shop.getShopid());
            }
            QueryWrapper<Item> queryWrapper2 = new QueryWrapper();
            List<Integer> list2 = new ArrayList<>();
            if (list.size() != 0) {
                  queryWrapper2.in("shopid", list);
                  for (Item item : itemDao.selectList(queryWrapper2)) {
                        list2.add(item.getId());
                  }
            }

            QueryWrapper<Swiperitem> queryWrapper = new QueryWrapper<>();
            if (list2.size() != 0) {
                  queryWrapper.like("slogan", slogan).in("itemid", list2);//联合查询
                  swiperitemDao.selectPage(swiperitemIPage, queryWrapper);
            }
            List<SwiperitemVO> swiperitemVOList = new ArrayList<>();
            for (Swiperitem swiperitem : swiperitemIPage.getRecords()) {
                  swiperitemVOList.add(entityToVO(swiperitem));
            }
            IPage<SwiperitemVO> swiperitemVOIPage = new Page<>(pageid, pagesize);
            swiperitemVOIPage.setTotal(swiperitemIPage.getTotal());
            swiperitemVOIPage.setRecords(swiperitemVOList);
            return CommonResponse.createForSuccess(swiperitemVOIPage);
      }


      @Override
      public CommonResponse addSwiperitem(Swiperitem swiperitem) {

            swiperitem.setClick(0);
            swiperitemDao.insert(swiperitem);
            return CommonResponse.createForSuccessMessage("新增轮播图成功");
      }

      @Override
      public CommonResponse deleteSwiperitem(int itemid) {

            swiperitemDao.deleteById(itemid);

            return CommonResponse.createForSuccessMessage("删除轮播图成功");
      }

      @Override
      public CommonResponse updateSwiperitem(Swiperitem swiperitem) {

            UpdateWrapper<Swiperitem> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("itemid", swiperitem.getItemid()).set("pictureid", swiperitem.getPictureid());
            updateWrapper.set("description", swiperitem.getDescription()).set("slogan", swiperitem.getSlogan());
            updateWrapper.set("starttime", swiperitem.getStarttime()).set("endtime", swiperitem.getEndtime()).set("available", swiperitem.getAvailable());

            swiperitemDao.update(null, updateWrapper);
            return CommonResponse.createForSuccessMessage("更新轮播图成功");
      }

      private SwiperitemVO entityToVO(Swiperitem swiperitem) {
            Picture picture = pictureDao.selectById(swiperitem.getPictureid());
            SwiperitemVO swiperitemVO = new SwiperitemVO();
            swiperitemVO.setItemid(swiperitem.getItemid());
            swiperitemVO.setPictureid(swiperitem.getPictureid());
            swiperitemVO.setSrc(picture.getSrc());
            swiperitemVO.setDescription(swiperitem.getDescription());
            swiperitemVO.setSlogan(swiperitem.getSlogan());
            swiperitemVO.setStarttime(swiperitem.getStarttime());
            swiperitemVO.setEndtime(swiperitem.getEndtime());
            swiperitemVO.setAvailable(swiperitem.getAvailable());
            swiperitemVO.setClick(swiperitem.getAvailable());
//            QueryWrapper queryWrapper = new QueryWrapper();
//            queryWrapper.eq("id",swiperitem.getItemid());
//            Item item = itemDao.selectOne(queryWrapper);

            Item item = itemDao.selectById(swiperitem.getItemid());
            swiperitemVO.setShopname(shopDao.selectById(item.getShopid()).getName());
            return swiperitemVO;
      }
}
